package com.intramirror.product.controller;

import com.intramirror.core.common.response.Response;
import com.intramirror.core.common.response.StatusCode;
import com.intramirror.product.service.IPromotionService;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created on 2018/4/10.
 * @author 123
 */
@RestController
public class SnapshotProductController {
    private final static Logger LOGGER = LoggerFactory.getLogger(SnapshotProductController.class);

    @Autowired
    IPromotionService promotionService;

    @RequestMapping(value = "/snapshot/{promotionId}/refresh", method = RequestMethod.PUT)
    public Response refresh(@PathVariable(value = "promotionId") Long promotionId) {
        LOGGER.info("Refresh Snapshot product for promotion {}.", promotionId);
        promotionService.refreshSnapshotProductByPromotion(promotionId);
        return Response.status(StatusCode.SUCCESS).data("ok");
    }

    @RequestMapping(value = "/snapshot/add/{productId}", method = RequestMethod.PUT)
    public Response addProductToSnapshot(@PathVariable(value = "productId") Long productId) {
        LOGGER.info("Add product {} to snapshot.", productId);
        promotionService.refreshSnapshotForAddProduct(productId);
        return Response.status(StatusCode.SUCCESS).data("ok");
    }

    @RequestMapping(value = "/snapshot/batch/add", method = RequestMethod.PUT)
    public Response batchAddProductToSnapshot(@RequestBody List<Long> productIds) {
        LOGGER.info("Batch add product {} to snapshot.", productIds.toArray());
        promotionService.refreshBatchSnapshotForAddProduct(productIds);
        return Response.status(StatusCode.SUCCESS).data("ok");
    }
}
